<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>参赛项目列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="projectName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">成绩</label>
                            <div class="layui-input-inline">
                                <input type="text" name="unit" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">比赛场所</label>
                            <div class="layui-input-inline">
                                <input type="text" name="place" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 添加比赛项目</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除比赛项目
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!--开关-->
        <!--<div class="layui-input-block" id="state">-->
            <!--<input type="checkbox" name="close" lay-skin="switch" lay-filter="filter" lay-text="已参赛完|未参赛完"-->
                   <!--lay-skin="switch">-->
        <!--</div>-->

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">查看</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<!--开关-->
<!--<script type="text/html" id="state">-->
    <!--<input type="checkbox" name="deleted" value="{{d.id}}" lay-skin="switch" lay-text="已参赛完|未参赛完" lay-filter="sexDemo" >-->
<!--</script>-->

<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
//状态开关 监听操作
//         form.on('switch(sexDemo)', function(obj){
//             layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
//         });

        //显示项目列表
        table.render({
            elem: '#currentTableId',
            // url: '../api/table.json',
            url: '/sport/project/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'id', width: 80, title: 'ID', sort: true},

                {field: 'projectName', width: 110, title: '参赛项目'},
                {field: 'projectTro', width: 110, title: '项目介绍', sort: true},
                {field: 'unit', width: 110, title: '成绩/单位', sort: true},
                {
                    field: 'genderLimit', width: 50, title: '性别限制', minWidth: 150,
                    templet: function (d) {
                        if (d.genderLimit ==1)
                            return '男';
                        else {
                            return '女';
                        }
                    }
                },

                {field: 'place', width: 110, title: '参赛场所', sort: true},
                {field: 'takeNum', width: 120, title: '报名总人数', sort: true},
                {field: 'date', width: 120, title: '比赛日期', sort: true},
                {field: 'startTime', width: 110, title: '开始时间'},
                // {field: 'deleted', width: 110, title: '删除状态', sort: true},
                {field: 'deleted', width: 110, templet: '#state', title: '参加状态', sort: true,align: "center",
                    templet: function (d) {
                        if (d.deleted == "1")
                            return '<a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">已参加</a>';
                        else {
                            return '<a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">未参加</a>';
                        }
                    }
                    },
                {field: 'endTime', width: 135, title: '结束时间', sort: true},
                {title: '操作', minWidth: 50, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);


            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });


        //参加状态的监听
        form.on('switch(filter)', function (data) {
            console.log(data.elem.value)
        })
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加参赛项目',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'project-add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    // title: '编辑参赛项目',
                    title: '查看参赛项目',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'project-edit.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    // obj.del();//页面删除的字段消失
                    $.getJSON("/sport/project/del", {id: obj.data.id}, function (res) {

                        if (res.code == 0) {
                            layer.alert(res.msg)
                        } else {
                            layer.alert(res.msg)
                        }

                    })
                    layer.close(index);
                });
            }
        });

    });
</script>
<script>

</script>

</body>
</html>